﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using SharedLib;

namespace TracyGUI.View.UIComponent
{
    public class EnhancedListView : ListView
    {
        private ListViewColumnSorter _lvwColumnSorter;
        private Dictionary<int, int> _columnTypeMapping = new Dictionary<int,int>();
        public Dictionary<int, int> ColumnTypeMapping
        {
            get { return _columnTypeMapping; }
        }

        protected override void InitLayout()
        {
            base.InitLayout();
            // Create an instance of a ListView column sorter and assign it 
            // to the ListView control.
            _lvwColumnSorter = new ListViewColumnSorter();
            this.ListViewItemSorter = _lvwColumnSorter;

        }

        public static ListViewItem.ListViewSubItem AddSubListViewItem(ListViewItem lvItem, string name, string text)
        {
            ListViewItem.ListViewSubItem lvSubItem = new ListViewItem.ListViewSubItem(lvItem, text);
            lvSubItem.Name = name;
            lvItem.SubItems.Add(lvSubItem);
            return lvSubItem;
        }

        public ColumnHeader AddColumn(string name, string text, int columnType = ListViewColumnSorter.COLUMN_TYPE_TEXT)
        {
            if (columnType != ListViewColumnSorter.COLUMN_TYPE_TEXT)
            {
                int index = Columns.Count;
                ColumnTypeMapping.Add(index, columnType);
            }
            ColumnHeader rtn = Columns.Add(text);
            rtn.Name = name;
            return rtn;
        }

        protected override void OnColumnClick(ColumnClickEventArgs e)
        {
            base.OnColumnClick(e);
            
            // Determine if clicked column is already the column that is being sorted.
            if (e.Column == _lvwColumnSorter.SortColumn)
            {
                // Reverse the current sort direction for this column.
                if (_lvwColumnSorter.Order == SortOrder.Ascending)
                {
                    _lvwColumnSorter.Order = SortOrder.Descending;
                }
                else
                {
                    _lvwColumnSorter.Order = SortOrder.Ascending;
                }
            }
            else
            {
                // Set the column number that is to be sorted; default to ascending.
                SetSortColumn(e.Column, SortOrder.Ascending);
            }

            // Perform the sort with these new sort options.
            this.Sort();
        }

        public void SetSortColumn(int columnIndex, SortOrder order = SortOrder.None)
        {
            _lvwColumnSorter.Order = order;
            _lvwColumnSorter.SortColumn = columnIndex;
            if (ColumnTypeMapping.ContainsKey(columnIndex))
            {
                _lvwColumnSorter.ColumnType = ColumnTypeMapping[columnIndex];
            }
        }

        public void SetSortColumn(int columnIndex, string order)
        {
            if (String.Equals(order, "desc", StringComparison.OrdinalIgnoreCase))
            {
                SetSortColumn(columnIndex, SortOrder.Descending);
            }
            else if (String.Equals(order, "asc", StringComparison.OrdinalIgnoreCase))
            {
                SetSortColumn(columnIndex, SortOrder.Ascending);
            }
            else
            {
                SetSortColumn(columnIndex);
            }
        }
    }

    
}
